package org.jeecg.modules.flow;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.flowable.entity.FlowCategory;
import org.jeecg.modules.flowable.mapper.FlowCategoryMapper;
import org.jeecg.modules.flowable.service.FlowCategoryService;
import org.jeecg.modules.flowable.service.FlowOperatorService;
import org.jeecg.modules.flowable.utils.BeanUtils;
import org.jeecg.modules.flowable.vo.FlowCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.jeecg.modules.flowable.dto.FlowRoleDTO;
import org.jeecg.modules.flowable.dto.FlowUserDTO;
import org.jeecg.modules.system.service.impl.SysRoleServiceImpl;
import org.jeecg.modules.system.service.impl.SysUserServiceImpl;
@Service
public class FlowOperatorServiceImpl  implements FlowOperatorService {

    @Autowired
    ISysBaseAPI sysBaseAPI;
    @Autowired
    SysUserServiceImpl sysUserService;
    @Autowired
    SysRoleServiceImpl sysRoleService;
    @Override
    public FlowUserDTO getLoginUser() {
        LoginUser sysUser = null;
        FlowUserDTO copyProperties = null;
        try {
            sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            copyProperties = BeanUtil.copyProperties(sysUser, FlowUserDTO.class);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return copyProperties;
    }

    @Override
    public List<FlowUserDTO> getAllUser() {
        List<org.jeecg.modules.system.entity.SysUser> list = sysUserService.list();
        List<FlowUserDTO> userList = list.stream().map(o -> BeanUtil.copyProperties(o, FlowUserDTO.class)).collect(Collectors.toList());
        return userList;
    }

    @Override
    public List<FlowUserDTO> getUsersByRoleId(String roleId) {
        Page<org.jeecg.modules.system.entity.SysUser> page = new Page<>(1,Integer.MAX_VALUE);
        IPage<org.jeecg.modules.system.entity.SysUser> userByRoleId = sysUserService.getUserByRoleId(page, roleId, null);
        List<org.jeecg.modules.system.entity.SysUser> records = userByRoleId.getRecords();
        List<FlowUserDTO> userList = records.stream().map(o -> BeanUtil.copyProperties(o, FlowUserDTO.class)).collect(Collectors.toList());
        return userList;
    }




    @Override
    public FlowUserDTO getUserByUsername(String username) {
        LoginUser userByName = sysBaseAPI.getUserByName(username);
        return userByName==null?null:BeanUtil.copyProperties(userByName, FlowUserDTO.class);
    }

    @Override
    public List<FlowRoleDTO> getAllRole() {
        List<org.jeecg.modules.system.entity.SysRole> list = sysRoleService.list();
        List<FlowRoleDTO> roleList = list.stream().map(o -> BeanUtil.copyProperties(o, FlowRoleDTO.class)).collect(Collectors.toList());
        return roleList;
    }

    @Override
    public List<String> getDepartNamesByUsername(String username) {
        List<String> departNamesByUsername = sysBaseAPI.getDepartNamesByUsername(username);
        return departNamesByUsername;
    }

    @Override
    public FlowUserDTO getUserById(String id) {
        org.jeecg.modules.system.entity.SysUser obj = sysUserService.getById(id);
        return obj==null?null:BeanUtil.copyProperties(obj, FlowUserDTO.class);
    }

    @Override
    public FlowRoleDTO getRoleById(String id) {
        org.jeecg.modules.system.entity.SysRole obj = sysRoleService.getById(id);
        return obj==null?null:BeanUtil.copyProperties(obj, FlowRoleDTO.class);
    }

    @Override
    public List<String> getRoleIdsByUsername(String username) {
        List<String> departNamesByUsername = sysBaseAPI.getRoleIdsByUsername(username);
        return departNamesByUsername;
    }


}
